package com.hexiang.rocketmq.consumer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQReplyListener;
import org.springframework.stereotype.Component;

/**
 * rocketmq 消息监听，@RocketMQMessageListener中的selectorExpression为tag，默认为*
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "RPC_HELLO_SPRING_TOPIC", consumerGroup = "demo-reply-topic-consumer-group-spring-message", selectorExpression = "*", selectorType = SelectorType.TAG)
public class DemoRocketMQReplyListener implements RocketMQReplyListener<MessageExt, MessageExt> {

    /**
     * @param messageExt data received by the listener
     * @return data replying to producer
     */
    @Override
    public MessageExt onMessage(MessageExt messageExt) {
        if(null!=messageExt){
            log.info("接收到普通消息：msg={}", new String(messageExt.getBody()));
            messageExt.setBody("this is reply message contents".getBytes());
        }
        return messageExt;
    }
}